package com.itmate.chenshiguang.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.itmate.chenshiguang.db.DBUtil;

import com.itmate.chenshiguang.entity.member;






public class memberdao extends BaseDao{

	/**
	 * 添加用户
	 * @param pu
	 * @return 返回修改数据库条数 0 失败 1成功
	 */
	public int addAccount(member pu){
			int count = 0;
		 try {
			connection = DBUtil.getConnection();
			String sql = "insert into member(username,password) values(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, pu.getName());
			preparedStatement.setString(2, pu.getPassword());
			count = preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		return count;
	}
	/**
	 * 判断用户名密码对应的数据是否存在
	 * @param pu
	 * @return 返回 true 存在， false不存在
	 */
	public boolean isExist(member pu){
		boolean flag = false;
		 try {
			 	int count = 0;
				connection = DBUtil.getConnection();
				String sql = "select count(*) as count from member where username=? and password=?";
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setString(1, pu.getUsername());
				preparedStatement.setString(2, pu.getPassword());
				resultSet = preparedStatement.executeQuery();
				while(resultSet.next()){
					//如果可以查找到id那么就说明该数据存在
					count = resultSet.getInt("count");
				}
				if (count != 0 ){
					flag = true;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				closed();
			}
		 return flag;
	}
	/**
	 * 判断账号密码是否正确
	 * @param u
	 * @return	true 正确，false 不整确
	 */
	public boolean login(member u) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from member where username = ? and password = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, u.getUsername());
			preparedStatement.setString(2, u.getPassword());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return flag;
	}
	/**
	 * 查询所有用户
	 * @return
	 */
	public List<member> selectAllUser(){
		List<member> accountlist = new ArrayList<>();
		try {
		 	
			connection = DBUtil.getConnection();
			String sql = "select * from member";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				member pu = new member();
				pu.setId(resultSet.getInt("id"));
				pu.setName(resultSet.getString("name"));
				pu.setUsername(resultSet.getString("username"));
				pu.setPassword(resultSet.getString("password"));
				pu.setFamily_id(resultSet.getInt("family_id"));
				accountlist.add(pu);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		return accountlist;
	}
	/**
	 * 判断用户名是否存在
	 * @param u
	 * @return	true 存在，false 不存在
	 */
	public boolean idExist(String username) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from member where username=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, username);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			closed();
		}
		return flag;
	}
	/**
	 * 根据家庭id查询所有家庭成员
	 * @param family_id
	 * @return
	 */
	public int selectMemberByFamily_id(int family_id) {
		int i = 0;		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select count(*) from member where family_id = ?");
			preparedStatement.setInt(1, family_id);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				i = resultSet.getInt("count");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return i;
	}
	/**
	 * 根据家庭id修改家庭名
	 * @param family_id
	 * @return
	 */
	public int updateNameByFamily_id(member u) {
		int i = 0;
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("update member set name = ? where family_id = ?");
			preparedStatement.setString(1, u.getName());
			preparedStatement.setInt(2, u.getFamily_id());
			i = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	/**
	 * 根据用户名查询id
	 * @param u
	 * @return
	 */
	public int selectIdByUsername(member u) {
		int id = 0;		
		try {
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement("select id from member where username = ?");
			preparedStatement.setString(1,u.getUsername());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				id = resultSet.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return id;
	}
}
